<?php
 import("@.ORG.Page"); 
//最爱操作类
class FavoriteAction extends Action{
    
    //新宠首页
    public function Index(){
       $nick=Session::get('uname');
       $uid=Session::get('uid');
       //默认显示产前标签 
        $con['flag']='bborn';
        $flags=D('favriouteflag')->where($con)->findAll();
      //获得之前添加的第一次列表
    //  $favorite=M('Favorite');
                 
       $list=$this->getPageList($uid);
           
      $favoriteList=$list['list'];
       foreach($favoriteList AS $key=>$val){
      $favoriteList[$key]['time']=date('Y-m-d H:i:s',$val['ctime']); 
     }
     $this->assign("page",$list['page']);
     $this->assign("favoriteList",$favoriteList); 
     $this->assign("flags",$flags);
     $this->assign('username',$nick);
     $this->display('Tpl/default/Favorite/Index.html'); 
    }
     
     //得到对应组的的标签内容
    
    public function getotherflags(){
      $flag=$_REQUEST['flag'];
      $con['flag']=$flag;
      $flagList=D('favriouteflag')->where($con)->findAll();  
      if($flagList){
      $this->ajaxReturn($flagList,'',1,'json');
      }
        $this->ajaxReturn(null,'',0,'json');  
    }
    
    //根据id得到标签名称
    public function getflag(){
      $id=$_REQUEST['id'];
      $flag=D('favriouteflag')->where('id='.$id)->field('event')->find();  
      
       $this->ajaxReturn(null,$flag,1,'json'); 
    }
    //根据id得到标签名称
    public function getflagByEvent(){
      $event=$_REQUEST['recoverItem'];
      $con['event']=$event;
      $flag=D('favriouteflag')->where($con)->findAll();  
      
       $this->ajaxReturn($flag[0],"",1,'json'); 
    }
    
    
    //添加最爱
    public function addFavorite(){
        //公共数据
        $data['content']=$_REQUEST['content'];
        $data['uid']=Session::get('uid');
        $data['cdate']=time();
        $data['item']=$_REQUEST['item'];
        $data['flag']=$_REQUEST['flag'];
        
         $con1['uid']=$data['uid'];
    
         $itemlist=D('favorite')->where($con1)->findAll();
          $bz=0;
        foreach($itemlist AS $val){
        if($val['item']==$data['item']){
            
         $eid=$val['id'];   
         $bz=1; 
         break;
        }
    }
    if($bz==1){
       $con2['id']=$eid; 
       
      $fid=D('favorite')->where($con2)->save($data);  
      $this->ajaxReturn($eid,'添加成功！',1,'json');
    }
      else{   
         
         $fid=D('Favorite','home')->add($data);
         
         $all['type']=2;
         $all['uid']=$data['uid'];
         $all['ctime']=$data['cdate'];
         $all['tid']=$fid; 
         D('summary')->add($all); 
         if($fid){
         $this->ajaxReturn($fid,'添加成功！',1,'json');
         }
       }  
         $this->ajaxReturn(null,'添加失败！',0,'json');
               
    }
    //删除某一条第一次
    public function delFavorite(){
      $id=$_REQUEST['id'];
      
      $is=D('favorite')->where('id='.$id)->delete(); 
      if($is){
         $this->ajaxReturn(null,'',1,'json'); 
        
      }
        $this->ajaxReturn(null,'',0,'json'); 
        
    }
      
    
    //响应编辑动作
    public function doEdit(){
        $condition['id']=6;
        $data['content']=$_POST['content'];
        //更新用户自定义的模版内容
        if($_POST['favor']){
            $Model=new Model();
            $name=$_POST['favor'];
            echo $name;
            $ret=$Model->execute("update t_favorite set name='".$name."' where id in (select fid from t_user_favorite where id=".$condition[id].")");
            echo '进入自定义修改页面了';
        }
        //更新用户输入的内容
        $Favor=M('userFavorite');
        $res=$Favor->data($data)->where($condition)->save();
        echo $res;        
    }
    
     public function getPageList($uid){
    //处理where条件
    
  //  $limit = isset( $limit )?$limit:$this->config->limitpage;
  //  $result = $this->where( $map )->field( $field )->order( $order )->find() ;
  
 	    $count=D('favorite')->where('uid='.$uid)->count();
   	    
		$p = new Page ( $count, 7);
        
		$list=D('favorite')->where('uid='.$uid)->limit($p->firstRow.','.$p->listRows)->order('id desc')->field()->findAll();
		$p->setConfig('header','条记录');
		$p->setConfig('prev',"<");
		$p->setConfig('next','>');
		$p->setConfig('first','<<');
		$p->setConfig('last','>>');
        $page = $p->show();
	//	$this->assign ( "page", $page );
	//	$this->assign ( "list", $list );
    $result['page']=$page;
    $result['list']=$list;
    return $result;
   }  
  public function getAjaxFavorite(){
    
     $uid=Session::get('uid');
   $list=$this->getPageList($uid);
 //  $favoriteList=$list['list'];
 //  foreach($favoriteList AS $key=>$val){
 //  $favoriteList[$key]['time']=date('Y-m-d H:i:s',$val['ctime']); 
 
 // } 
  // $this->assign("page",$list['page']);
  // $this->assign("favoriteList",$favoriteList); 
   
     $this->ajaxReturn($list,'',1,'json');
}
}

?>